package com.ebs.md.api.abs;

/**
 * Revision Author:
 *  $File: //depot/MarketData/commons-md-api/MAIN/src/main/java/com/ebs/md/api/abs/BasicBookEntry.java $
 *  $Revision: #3 $
 *  $DateTime: 2012/07/04 13:24:46 $
 *  $Change: 112230 $
 *  $Author: Arik.Sher $
 */

import java.util.concurrent.atomic.AtomicLong;

import com.ebs.md.api.BookSide;
import com.ebs.md.api.IAggregatedByPriceBookEntry;
import com.ebs.md.api.Rate;


public class BasicBookEntry
implements IAggregatedByPriceBookEntry
{
	// gened via Eclipse
	private static final long serialVersionUID = 1843465640102064801L;

	protected BookSide    side;
	protected Rate        rate;
	protected AtomicLong  amount;

	public BasicBookEntry(BookSide side, Rate rate, long amount) {
		super();
		this.side   = side;
		this.rate   = rate;
		this.amount = new AtomicLong(amount);
	}
	
	public BookSide getSide() {
		return side;
	}
	public Rate getRate() {
		return rate;
	}
	public Long getAmount() {
		return amount.get();
	}

	// generated by Eclipse on "price"
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((rate == null) ? 0 : rate.hashCode());
		return result;
	}

	// generated by Eclipse on "rate"
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BasicBookEntry other = (BasicBookEntry) obj;
		if (rate == null) {
			if (other.rate != null)
				return false;
		} else if (!rate.equals(other.rate))
			return false;
		return true;
	}
	
	public void increaseAmount(long value) {
		amount.addAndGet(value);
	}

	public boolean decreaseAmount(long value) {
		long newVal = amount.addAndGet((-1) * value);
		
		return (newVal >= 1);
	}

	public Object clone() throws CloneNotSupportedException {
		BasicBookEntry be = new BasicBookEntry(side, rate, amount.get());
		return be;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		sb.append( "rate=");
		sb.append(  rate);
		sb.append(",amount=");
		sb.append(  amount);
		sb.append(",side=");
		sb.append(  side);
		
		return sb.toString();
	}

}
